package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.serivce.MqMessageService;
import com.tanhua.server.serivce.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService smallVideosService;

    /**
     * 发布视频
     *   post
     *   请求参数：
     *     两个文件：videoThumbnail-图片（客户端根据视频自动生成的封面图），videoFile-视频
     */
    @PostMapping
    public ResponseEntity smallVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        smallVideosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询视频列表
     *    get
     *    参数：page,pagesize
     *    返回值：PageResult  -- 封装List<VideosVo>
     */
    @GetMapping
    public ResponseEntity smallVideos(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize) {
        //1、调用service
        PageResult vo = smallVideosService.findVideos(page,pagesize);
        //2、返回
        return ResponseEntity.ok(vo);
    }

    /**
     * 视频点赞
     *   POST       /smallVideos/:id/like
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String videoId) {
        Integer count = smallVideosService.like(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频点赞 - 取消
     *   POST      /smallVideos/:id/dislike
     *   参数  视频id
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String videoId) {
        Integer count = smallVideosService.dislike(videoId);
        return ResponseEntity.ok(count);
    }


    /**
     *接口名称：评论列表
     * 接口路径：GET/smallVideos/:id/comments
     * 请求参数 id
     * Query：
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity findComments(@PathVariable("id") String videoId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = smallVideosService.findComments(page,pagesize,videoId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 视频评论
     * Post  /smallVideos/:id/comments
     * 参数   视频id
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveVideoComments(@PathVariable("id") String videoId,
                                            @RequestBody Map map) {
        String content = (String) map.get("comment");
        smallVideosService.saveVideoComments(videoId,content);
        return ResponseEntity.ok(null);
    }

}
